﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Linq;

namespace MyHealthByDesignWebsite.Models
{
    public class FoodEntry
    {
        private FullDataSetDataContext db = new FullDataSetDataContext();

        [Required]
        public int U_userID { get; set; }

        [Required]
        public string NDB_No { get; set;}

        public string UFJ_quantity { get; set; }

        public DateTime UFJ_date { get; set;}

        public string UFJ_mealTime { get; set; }

        /// <summary>
        /// Inserts a new record into 'UserFoodJournal' Table
        /// </summary>
        /// <param name="userFoodJournal"></param>
        public void Create(UserFoodJournal userFoodJournal)
        {
            db.UserFoodJournals.InsertOnSubmit(userFoodJournal);
            db.SubmitChanges();

        }

        /// <summary>
        /// Gets the list of 'UserFoodJournal' records for a user
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns>IQueryable<UserFoodJournal></returns>
        public IQueryable<UserFoodJournal> DetailsByUserID(int UserID)
        {
            return from m in db.UserFoodJournals
                   where m.U_userID.Equals(UserID)
                   select m;

        }

        /// <summary>
        /// Updates a record in 'UserFoodJournal' table
        /// </summary>
        /// <param name="userFoodJournal"></param>
        public void Update(UserFoodJournal userFoodJournal)
        {
            FullDataSetDataContext dc = new FullDataSetDataContext();

            var userJournal = from u in dc.UserFoodJournals
                              where u.U_userID == userFoodJournal.U_userID &&
                              u.NDB_No == userFoodJournal.NDB_No
                              select u;
            foreach (var item in userJournal)
            {
                item.U_userID = userFoodJournal.U_userID;
                item.NDB_No = userFoodJournal.NDB_No;
                item.UFJ_quntity = userFoodJournal.UFJ_quntity;               
                item.UFJ_date = userFoodJournal.UFJ_date;
                item.UFJ_mealTime = userFoodJournal.UFJ_mealTime;
                
            }
            dc.SubmitChanges();
        }
        
    }
}